package com.tycmc.signelectroniccontracts.logic;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.ActLogin;
import com.tycmc.signelectroniccontracts.act.htong.ActHtList;
import com.tycmc.signelectroniccontracts.act.renzheng.ActFirstName;
import com.tycmc.signelectroniccontracts.tools.base.LogicParent;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;

import java.util.Map;

public class PreLogin extends LogicParent {
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            handler.sendEmptyMessageDelayed(message.what - 1, 1000);//设置1s延迟
            pre.edCode(message.what);

            return true;
        }
    });
    private LoginInterface pre;
    private ActLogin context;

    public PreLogin(LoginInterface pre) {
        this.pre = pre;
        context = ((ActLogin) pre);
    }

    public void getcode(String modile) {
        if (TextShow.isEmpty(modile)) {
            ToastShow.popupToast("请输入手机号");
            return;
        }

        RequestAction action = new RequestAction(NetUrl.send);
        action.putBody("GroupID", "");
        action.putBody("Mobile", modile);
        action.putBody("SMSType", "1");//手机登录
        action.setContentType(action.APPLICATIONURLENCODED);
        final ProDialog dialog = new ProDialog(context, "正在发送验证码");
        dialog.show();
        new NetWorkUtils(context).post(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                handler.sendEmptyMessage(60);
            }

            @Override
            public void failed(Object data) {
                handler.sendEmptyMessage(60);
                dialog.dismiss();
            }
        });

    }


    public void getUserInfo() {
        RequestAction action = new RequestAction(NetUrl.getUserInfo);
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).getRequest(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                if(!TextShow.isEmpty(TextShow.valueOf(data.get("Message")))){
                    ToastShow.popupToast(TextShow.valueOf(data.get("Message")));
                    return;
                }
                SharePre.getEditor().putString(SharePreName.clientArchiveNo, TextShow.valueOf(data.get("Acnt_Client_ID"))).commit();
                query();

            }

            @Override
            public void failed(Object data) {
                ToastShow.popupToast(data+"");
                dialog.dismiss();
            }
        });
    }

    public void destory() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);//移除handler倒计时
        }
    }

    /**
     * 下一步
     *
     * @param account  手机号
     * @param code     验证码
     * @param ispeople 是否是个人
     */
    public void next(String account, String code, boolean ispeople) {
        if (TextShow.isEmpty(account)) {
            ToastShow.popupToast(context.getString(R.string.please_input_phonenumber));
            return;
        }
        if (TextShow.isEmpty(code)) {
            ToastShow.popupToast(context.getString(R.string.please_input_yanzhengma));
            return;
        }
        login(account, code);
//        SharePre.getEditor()
//                .putString(SharePreName.modile, account)
//                .putString(SharePreName.clientArchiveNo, "zs0001")
//                .putString(SharePreName.TOKEN, "eyJhbGciOiJSUzI1NiIsImtpZCI6IkE0MDc1MDAwRUNFMTVDRDZBMjY3N0M0QUJDNUMxNTMwRDYzRDRCREYiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJwQWRRQU96aFhOYWlaM3hLdkZ3Vk1OWTlTOTgifQ.eyJuYmYiOjE1Nzg1MzE5MTAsImV4cCI6MTU4MTEyMzkxMCwiaXNzIjoiaHR0cDovL3NvZnQudHlncHMuY29tL29hdXRoIiwiYXVkIjpbImh0dHA6Ly9zb2Z0LnR5Z3BzLmNvbS9vYXV0aC9yZXNvdXJjZXMiLCJhcGkiLCJjc3JhcGkiLCJyZW50YWxhcGkiXSwiY2xpZW50X2lkIjoicmVudGFsd2ViIiwic3ViIjoidHltaWNzXzEwMDAwNjQ1NjkiLCJhdXRoX3RpbWUiOjE1Nzg1MzE5MTAsImlkcCI6ImxvY2FsIiwiYWNudF9pZCI6IjEwMDAwNjQ1NjkiLCJncm91cF9pZCI6InR5bWljcyIsInN5c2lkIjoiMjAiLCJvcHJ0X25hbWUiOiLmnZzmiJjot4MiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwiYXBpIiwiY3NyYXBpIiwicmVudGFsYXBpIl0sImFtciI6WyJwYXNzd29yZCJdfQ.surAoh6MXQOOYx0hzaQ90N5by29x4pzeHKY3XfXmIt7YIwmfdudQ8nXZBxrktaoUZPI2vhy5POG9r0B_pamJ0ij2GGvyg87YYMCnG14njosgV_ECQ5L4FyJvbOPX1Oga8Z9L_nRSjqYgLCnlyoEsGkGdJzXE6YYsINXa9V7mvmemGxo0vWjMuBeK1p2Z_mX7wBMYoexoX8yrE7_tHmYJ3B5soJh6hPKchcpiHxpBZ9uFxTGxtH1RzQMyRqevkKGKqsAXt-2oFDnmCiH-N6XOmEXqzTVxG7ZZFgx0WUn_O-1zEFJ6DeLUEXbO94dYNd4XXI7OmQ2RnfohfVNZ0Db72w")
//                .commit();
//
//        query(SharePre.getString(SharePreName.clientArchiveNo, ""));
    }


    public void login(final String account, String code) {
        RequestAction action = new RequestAction(NetUrl.login);
        action.putBody("grant_type", "mobile");
        action.putBody("sysId", "20");
        action.putBody("client_id", "rentalweb");
        action.putBody("client_secret", "123456");
        action.putBody("scope", "csrapi api profile openid offline_access");
        action.putBody("mobile", account);
        action.putBody("code", code);
        action.setContentType(action.APPLICATIONURLENCODED);
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).post(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                if (!TextShow.isEmpty(TextShow.valueOf(data.get("error_description")))) {
                    ToastShow.popupToast(TextShow.valueOf(data.get("error_description")));
                    return;
                }
                SharePre.getEditor().putString(SharePreName.modile, account).putString(SharePreName.TOKEN, TextShow.valueOf(data.get("access_token"))).putString(SharePreName.token_type, TextShow.valueOf(data.get("token_type"))).commit();
                getUserInfo();
            }

            @Override
            public void failed(Object data) {
                ToastShow.popupToast(data+"");
                dialog.dismiss();
            }
        });
    }

    public void query() {
        RequestAction action = new RequestAction(NetUrl.whetherCertification);
        action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                String isCertification = TextShow.valueOf(data.get("isCertification"));
                String clientTypeID = TextShow.valueOf(data.get("clientTypeID"));
                String businessCode = TextShow.valueOf(data.get("clientNo"));
                SharePre.getEditor().putString(SharePreName.clientTypeID, clientTypeID).putString(SharePreName.businessCode, businessCode).commit();
                if (isCertification.equals("1")) {
                    context.start(new Intent(context, ActHtList.class));
                    context.exit();
                } else {
                    context.start(new Intent(context, ActFirstName.class));
//                    context.start(new Intent(context, ActHtList.class));
                }
            }

            @Override
            public void failed(Object data) {
                dialog.dismiss();
//                context.start(new Intent(context, ActFirstName.class));
                ToastShow.popupToast(data+"");
            }
        });
    }

    public interface LoginInterface {
        void edCode(int number);//倒计时
    }
}
